package com.zzzy.cont.controller.processcon;

import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.cont.dao.entity.ConActivityNote;
import com.zzzy.cont.service.ConActivityNoteService;
import com.zzzy.cont.service.processcon.ProcessConService;
import com.zzzy.cont.vo.contract.ConActivityNoteAddReq;
import com.zzzy.cont.vo.contract.my.ConBasicInfoRes;
import com.zzzy.cont.vo.contract.my.ConInfoRes;
import com.zzzy.cont.vo.contract.process.ProcessConStatisticsRes;
import com.zzzy.cont.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

@Tag(name = "办理合同", description = "con200")
@Validated
@RestController
@RequestMapping("/con/process")
public class ProcessConController {

    @Resource
    private ProcessConService processConService;
    @Resource
    private ConActivityNoteService conActivityNoteService;


    @GetMapping("/getConBasicInfo/{id}")
    @Operation(summary = "获取合同基本信息")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<ConBasicInfoRes> getConBasicInfo(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        ConBasicInfoRes con = processConService.getConBasicInfo(id);
        return AjaxResult.success(con);
    }

    @GetMapping(value = "/getPageOfficeFile/{id}")
    @Operation(summary = "打开文件")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<OpenOfficeFileRes> getPageOfficeFile(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/con/process/getConFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = processConService.getOpenOfficeResByConId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getConFileStream/{id}")
    @Operation(summary = "获取文件流")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public void getConFileStream(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        ConInfoRes conInfo = processConService.getConInfo(id);
        byte[] fileBlob = Objects.isNull(conInfo.getPdfFileBlob()) ? conInfo.getWordFileBlob() :conInfo.getPdfFileBlob();
        FileUtil.outputFileStream(response, fileBlob, conInfo.getName() + "." + FileUtil.getFileType(fileBlob));
    }

    @Operation(summary = "活动记录列表")
    @GetMapping(value = "/activityNoteList/{id}")
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<List<ConActivityNote>> activityNoteList(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        List<ConActivityNote> activityNoteList = conActivityNoteService.getActivityNoteListByConId(id);
        return AjaxResult.success(activityNoteList);
    }

    @PostMapping("/addActivityNote")
    @Operation(summary = "新增活动记录")
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult addActivityNote(@RequestBody @Valid ConActivityNoteAddReq addReq) {
        conActivityNoteService.addUserNote(addReq.getId(), addReq.getContent());
        return AjaxResult.success();
    }

    @Operation(summary = "统计信息")
    @GetMapping(value = "/getStatistics")
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<ProcessConStatisticsRes> getStatistics() {
        ProcessConStatisticsRes statistics = processConService.getStatistics();
        return AjaxResult.success(statistics);
    }
}
